package com.znf4.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.core.NamedThreadLocal;

import com.alibaba.fastjson.JSONObject;

public class BaseReturnModel { // model 模型
	
	private static final ThreadLocal<Map<String, Object>> mapThreadLocal = new NamedThreadLocal<Map<String, Object>>("ThreadLocal mapReturnModel");
	
	private static final ThreadLocal<JSONObject> jsonThreadLocal = new NamedThreadLocal<JSONObject>("ThreadLocal jsonReturnModel");

	/**
	 * 函数功能说明 ： 后台所有接口都返回json 数据，封装map 转json
	 * 
	 * @param state  success/faild
	 * @param data   list/string/int/....
	 * @param error  错误信息描述
	 * @return
	 */
	public static Map<String, Object> getMapModel(Object state, Object data, Object error) {
		Map<String, Object> returnMapModel = new HashMap<String, Object>();
		returnMapModel.put("STATE", state);
		returnMapModel.put("DATA", data);
		returnMapModel.put("ERROR", error);
		mapThreadLocal.set(returnMapModel);
		return returnMapModel ;

	}
	
	
	/**
	 * 函数功能说明 ： 追加map其他返回值
	 * 
	 * @param key  必须大写
	 * @param data
	 * @return
	 */
	public static void setMapModel(String key, Object data ) {

		if(key != null){
			mapThreadLocal.get().put(key , data);
		}

	}
	
	
	
	/**
	 * 函数功能说明 ： 后台所有接口都返回json 数据，封装json 
	 * 
	 * @param state  success/faild
	 * @param data   list/string/int/....
	 * @param error  错误信息描述
	 * @return
	 */
	public static JSONObject getJsonModel(Object state, Object data, Object error) {
		JSONObject json = new JSONObject();
		json.put("STATE", state);
		json.put("DATA", data);
		json.put("ERROR", error);
		jsonThreadLocal.set(json);
		
		return json ;

	}
	
	/**
	 * 函数功能说明 ： 追加json中其他返回值
	 * 
	 * @param key  必须大写
	 * @param data
	 * @return
	 */
	public static void setJsonModel(String key, Object data ) {

		if(key != null){
			jsonThreadLocal.get().put(key , data);
		}

	}
	
	
	
	public static void main(String[] args) {
		
		//[{"DATA":"data","STATE":"state"}] 这种格式的json
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>() ;
		Map<String, Object> returnMapModel = new HashMap<String, Object>();
		returnMapModel.put("STATE", "state");
		returnMapModel.put("DATA", "data");
		list.add(returnMapModel);
		
		System.out.println(JSONObject.toJSONString(list));
		
	}
	
	
	
	

}
